<template>
  <div class="wrapper">
    <v-head></v-head>
    <v-sidebar></v-sidebar>
    <div class="content-box" :class="{'content-collapse':collapse}">
      <v-tags></v-tags>
      <div class="content">
        <transition name="move" mode="out-in">
          <keep-alive :include="tagsList">
            <router-view></router-view>
          </keep-alive>
        </transition>
        <el-backtop target=".content"></el-backtop>
      </div>
    </div>
  </div>
</template>

<script>
import vHead from './Header.vue';
import vSidebar from './Sidebar.vue';
import vTags from './Tags.vue';
import bus from './bus';
import { token } from '../../api/index';
import Vue from 'vue';

export default {
  data () {
    return {
      tagsList: [],
      collapse: false
    };
  },
  components: {
    vHead,
    vSidebar,
    vTags
  },
  beforeRouteEnter (to, from, next) {
    token()
      .then(data => {
        if (data.code === 401 || data.code === 403) {
          localStorage.removeItem('ad_Authorization');
          Vue.prototype.$message.error(`${data.msg}`);
          next({ path: '/login' });
          // next(vm => {
          //     vm.$message.error('您的登录状态已失效，请先登录！');
          //     next({ path: '/login' });
          // });
        } else if (!data.success) {
          localStorage.removeItem('ad_Authorization');
          Vue.prototype.$message.error(`${data.msg}`);
          next({ path: '/login' });
          // next(vm => {
          //     vm.$message.error(`${data.msg}`);
          //     next({ path: '/login' });
          // });
        } else {
          let clientWidth = document.body.clientWidth

          if (clientWidth > 750) {
            next();
          } else {
            loseMenu().then(data => {
              if (data.success) {

                if (data.data) {
                  window.location.href = this.url + "/m/#/unemploy"
                } else {
                  window.location.href = this.url + "/m/#/dataanalysis"
                }




              }
            });
            // window.location.href = this.url + "/m/#/dataanalysis"
          }

        }
      })
      .catch(error => {
        localStorage.removeItem('ad_Authorization');
        Vue.prototype.$message.error('您的登录状态已失效，请先登录！');
        next({ path: '/login' });
        // next({ path: '/login' });
      });
  },
  created () {
    bus.$on('collapse-content', msg => {
      this.collapse = msg;
      this.$nextTick(() => {
        // bus.$emit('resize')
        bus.$emit('optionPiesSize')
      })
    });

    // 只有在标签页列表里的页面才使用keep-alive，即关闭标签之后就不保存到内存中了。
    bus.$on('tags', msg => {
      console.log(msg);
      let arr = [];
      for (let i = 0, len = msg.length; i < len; i++) {
        msg[i].name && arr.push(msg[i].name);
        console.log(msg[i].name && arr.push(msg[i].name));
      }
      this.tagsList = arr;
      console.log(this.tagsList);

    });
  }
};
</script>
